package annotations;

import java.lang.reflect.Method;

public class ImprovedJUnitRunner {
    public static void main(String[] args) throws Exception {
        Class<?> testClass = MyTestCases.class;
        Object testInstance = testClass.getDeclaredConstructor().newInstance();

        int total = 0;
        int passed = 0;
        int failed = 0;

        System.out.println("=== 测试开始 ===");

        for (Method method : testClass.getDeclaredMethods()) {
            if (method.isAnnotationPresent(MyTest.class)) {
                total++;
                String methodName = method.getName();

                try {
                    long startTime = System.currentTimeMillis();
                    method.invoke(testInstance);
                    long duration = System.currentTimeMillis() - startTime;

                    System.out.printf("[✓] %s - 通过 (耗时: %dms)\n", methodName, duration);
                    passed++;
                } catch (Exception e) {
                    Throwable cause = e.getCause() != null ? e.getCause() : e;
                    System.out.printf("[×] %s - 失败: %s\n", methodName, cause.getMessage());
                    failed++;
                }
            } else {
                // 统计未加注解的方法（可选）
                System.out.printf("[ ] %s - 未标记为测试方法\n", method.getName());
            }
        }

        System.out.println("\n=== 测试统计 ===");
        System.out.printf("总测试方法: %d\n", total);
        System.out.printf("通过: %d\n", passed);
        System.out.printf("失败: %d\n", failed);
        System.out.printf("通过率: %.1f%%\n", total > 0 ? (passed * 100.0 / total) : 0);
    }
}